<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="100px">

      <el-form-item label="姓名" prop="userName">
        <el-input
          v-model="queryParams.userName"
          placeholder="请输入用户姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="姓名" prop="staffName">
        <el-input
          v-model="queryParams.staffName"
          placeholder="请输入姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="打卡类型" prop="clockTypeName">
        <el-input
          v-model="queryParams.clockTypeName"
          placeholder="请输入打卡类型"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList" :columns="columns"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="clockbindingList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
<!--      <el-table-column label="用户姓名" align="center" prop="userName" />-->
      <el-table-column label="姓名" align="center" prop="staffName" v-if="columns[0].visible"/>
      <el-table-column label="性别" align="center" prop="sex" v-if="columns[1].visible">
        <template  slot-scope="scope">
          <span v-if="scope.row.sex==0">女</span>
          <span v-if="scope.row.sex==1">男</span>
        </template>
      </el-table-column>
      <el-table-column label="年龄" align="center" prop="age" v-if="columns[2].visible"/>
      <el-table-column label="电话号码" align="center" prop="phoneNumber" v-if="columns[3].visible"/>

      <el-table-column label="打卡类型" align="center" prop="clockTypeName" v-if="columns[4].visible"/>
      <el-table-column label="备注" align="center" prop="remark" v-if="columns[5].visible"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['module:clockbinding:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['module:clockbinding:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改员工与打卡类型绑定对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="姓名" prop="staffName">
          <el-input v-model="form.staffName" placeholder="请输入姓名" />
        </el-form-item>
        <el-form-item label="性别" prop="sex">
          <el-input v-model="form.sex" placeholder="请输入年龄" />
        </el-form-item>
        <el-form-item label="年龄" prop="age">
          <el-input v-model="form.age" placeholder="请输入年龄" />
        </el-form-item>
        <el-form-item label="电话号码" prop="phoneNumber">
          <el-input v-model="form.phoneNumber" placeholder="请输入电话号码" />
        </el-form-item>


<!--        <el-form-item label="打卡类型id" prop="clockTypeId">-->
<!--          <el-input v-model="form.clockTypeId" placeholder="请输入打卡类型id" />-->
<!--        </el-form-item>-->
        <el-form-item label="打卡类型" prop="clockTypeName" >
          <el-input @change="clearClockType" v-model="form.clockTypeName" placeholder="请输入打卡类型" clearable>
            <el-button slot="append" @click="typeFn" >选择打卡类型</el-button>
          </el-input>
        </el-form-item>

        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel" >取 消</el-button>
      </div>
    </el-dialog>
    <el-dialog title="打卡类型" :visible.sync="typeOpen" width="60%">
      <el-table
        :data="typeOptions"
        style="width: 100%">
        <el-table-column type="expand">
          <template slot-scope="scope">
          <div class="type_time" v-for="(item,index) in scope.row.scopeList" :key="index">
            <div class="type_start">
              <div class="type_title">起始时间</div>
              <div class="type_times">{{item.startTime}}</div>
            </div>
            <div class="type_start">
              <div  class="type_title">终止时间</div>
              <div  class="type_times">{{item.endTime}}</div>
            </div>

          </div>
          </template>
        </el-table-column>
        <el-table-column
          label="打卡类型"
          prop="typeName">
        </el-table-column>
        <el-table-column
          label="打卡半径"
          prop="scope">
        </el-table-column>
        <el-table-column
          label="打卡地点"
          prop="addressName">
        </el-table-column>
        <el-table-column
          align="right">
          <template slot="header" slot-scope="scope">
            <el-input

              size="mini"
              placeholder="输入关键字搜索"/>
          </template>
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="primary"
              @click="typeSubmitForm(scope.row)"
              >Select</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-dialog>
  </div>
</template>

<script>

import { listClockbinding, getClockbinding, delClockbinding, addClockbinding, updateClockbinding } from "@/api/module/clockbinding";
import {listClockedtype} from "@/api/module/clockedtype";

export default {
  name: "Clockbinding",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 员工与打卡类型绑定表格数据
      clockbindingList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      typeOpen:false,//是否弹出打卡类型框
      typeOptions:[],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userId: null,
        userName: null,
        staffId: null,
        staffName: null,
        clockTypeId: null,
        clockTypeName: null,
      },
      // 表单参数
      form: {},
      // 列信息
      columns: [
        { key: 0, label: `员工名`, visible: true },
        { key: 1, label: `性别`, visible: true },
        { key: 2, label: `年龄`, visible: true },
        { key: 3, label: `电话号码`, visible: true },
        { key: 4, label: `打卡类型`, visible: true },
        { key: 5, label: `备注`, visible: true },

      ],
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
    this.getTypeList()
  },
  methods: {
    typeFn(){
      console.log('11')
      this.typeOpen=true
    },
    getTypeList(){
      listClockedtype().then(res=>{
        console.log(res)
        this.typeOptions=res.rows
      })
    },
    typeSubmitForm(item){
      console.log(item)
      this.form.clockTypeName=item.typeName
      this.form.clockTypeId=item.id
      this.typeOpen=false

    },
    clearClockType(val){
      if (val==null || val===""){
        this.form.clockTypeName=null
        this.form.clockTypeId=null
      }
    },
    typeCancel(){
      this.typeOpen=false
    },
    /** 查询员工与打卡类型绑定列表 */
    getList() {
      this.loading = true;
      listClockbinding(this.queryParams).then(response => {
        this.clockbindingList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        userId: null,
        userName: null,
        staffId: null,
        staffName: null,
        clockTypeId: null,
        clockTypeName: null,
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加员工与打卡类型绑定";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getClockbinding(id).then(response => {
        console.log(response)
        this.form = response.data;
        this.open = true;
        this.title = "修改员工与打卡类型绑定";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateClockbinding(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addClockbinding(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除员工与打卡类型绑定编号为"' + ids + '"的数据项？').then(function() {
        return delClockbinding(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('module/clockbinding/export', {
        ...this.queryParams
      }, `clockbinding_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
<style>
.type_time{
  display: flex;
  margin-left: 100px;
  margin-bottom: 5px;
  /*justify-content: space-around;*/
}
.type_start{
  display: flex;
  width: 30%;
  height: 50px;
  border: solid 1px #eee;
  text-align: center;
  align-items: center;

}
.type_title{
  width: 30%;
  height: 50px;
  line-height: 50px;
  background-color: #eee;
  border-right: solid 1px #eee;
}
.type_times{
  width: 50%;
}

</style>
